﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GotLyrics
{
    public partial class WordCasingOptionsControl : UserControl
    {
        public WordCasingOptionsControl()
        {
            InitializeComponent();
        }

        public WordCasingType CasingType
        {
            get
            {
                if (rbWordCasingNone.Checked)
                    return WordCasingType.None;
                else if (rbWordCasingLowercase.Checked)
                    return WordCasingType.LowerCase;
                else if (rbWordCasingUpperCase.Checked)
                    return WordCasingType.UpperCase;
                else if(rbWordCasingCapAllWords.Checked)
                    return WordCasingType.CapitalizeEachWord;
                else // (rbWordCasingCapFirstWord.Checked)
                    return WordCasingType.CapitalizeFirstWord;
            }
            set
            {
                RadioButton rb = null;

                switch (value)
                {
                    case WordCasingType.CapitalizeEachWord:
                        rb = rbWordCasingCapAllWords;
                        break;

                    case WordCasingType.CapitalizeFirstWord:
                        rb = rbWordCasingCapFirstWord;
                        break;

                    case WordCasingType.LowerCase:
                        rb = rbWordCasingLowercase;
                        break;

                    case WordCasingType.None:
                        rb = rbWordCasingNone;
                        break;

                    case WordCasingType.UpperCase:
                        rb = rbWordCasingUpperCase;
                        break;
                }

                rb.Checked = true;
            }
        }

        public static string Case(string text, WordCasingType casingType)
        {
            switch (casingType)
            {
                case WordCasingType.CapitalizeEachWord:
                    if (text.Length == 1)
                        text = text.ToUpperInvariant();
                    else
                    {
                        string[] words = text.Split(" ".ToCharArray());

                        text = "";

                        foreach (string word in words)
                        {
                            if (word.Length == 1)
                                text += word.ToUpperInvariant();
                            else
                            {
                                text += word[0].ToString().ToUpperInvariant() +
                                         word.Substring(1);
                            }

                            text += " ";
                        }

                        text = text.Trim();
                    }
                    break;

                case WordCasingType.CapitalizeFirstWord:
                    if (text.Length == 1)
                        text = text.ToUpperInvariant();
                    else
                    {
                        text = text[0].ToString().ToUpperInvariant() +
                                text.Substring(1);
                    }
                    break;

                case WordCasingType.LowerCase:
                    text = text.ToLowerInvariant();
                    break;

                case WordCasingType.UpperCase:
                    text = text.ToUpperInvariant();
                    break;
            }

            return text;
        }
    }

    /// <summary>
    /// Word casing options.
    /// </summary>
    public enum WordCasingType
    {
        /// <summary>
        /// No casing.
        /// </summary>
        None,
        /// <summary>
        /// Convert all to lowercase.
        /// </summary>
        LowerCase,
        /// <summary>
        /// Convert all to uppercase.
        /// </summary>
        UpperCase,
        /// <summary>
        /// Capitalize each word.
        /// </summary>
        CapitalizeEachWord,
        /// <summary>
        /// Capitalize only the first word.
        /// </summary>
        CapitalizeFirstWord
    }
}
